package com.sinai.mshab.extensions.security;

import java.util.Date;

import com.google.gwt.user.client.Cookies;

/**
 * This class stores the cookies with the user's sessionID that was given by the
 * server.
 * 
 * @author serge
 * 
 */
public class Security {
	
	private static boolean loggedIn = false;

	/**
	 * Creates a cookie remembering the user's sessionID.
	 * 
	 * @param sessionID
	 *            the session ID given by the server.
	 * @param duration
	 *            the duration in hours
	 */
	public static void rememberLogin(String sessionID, int duration) {
		final int expire_duration = 1000 * 60 * 60;
		Date expires = new Date(System.currentTimeMillis() + expire_duration);
		Cookies.setCookie("sid", sessionID, expires, null, "/", false);
	}

	/**
	 * Returns the sessionID associated with the user, or null if the cookie
	 * expired or does not exist.
	 * 
	 * @return the sessionID associated with the user, or null
	 */
	public static String getSessionID() {
		return Cookies.getCookie("sid");
	}
	
	/**
	 * Removes any cookies associated with this page.
	 */
	public static void logOff() {
		setLoggedIn(false);
		Cookies.removeCookie("sid");
	}
	
	/**
	 * @return Returns if true if the user is logged in.
	 */
	public static boolean isLoggedIn() {
		return loggedIn;
	}
	
	/**
	 * @param log the new value of logged in.
	 */
	public static void setLoggedIn(boolean log) {
		loggedIn = log;
	}
}
